package com.wuster.utils;


import java.sql.Date;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.temporal.TemporalAdjusters;


/**
 * 提取的方法，在多个地方用到
 */
public class CommonUtils {



    /**
     * 根据weekId得到本周或下周的五个日期
     * @param weekId
     * @return
     */
    public static Date[] getDateByWeekId(Integer weekId){
        // 获取当前日期
        LocalDate today, monday, tuesday, wednesday, thursday, friday;
        today = monday = tuesday = wednesday = thursday = friday = LocalDate.now();

        if (weekId == 0){
            // 获取本周的周一和周五的日期
            monday = today.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY));
            tuesday = today.with(TemporalAdjusters.previousOrSame(DayOfWeek.TUESDAY));
            wednesday = today.with(TemporalAdjusters.previousOrSame(DayOfWeek.WEDNESDAY));
            thursday = today.with(TemporalAdjusters.previousOrSame(DayOfWeek.THURSDAY));
            friday = today.with(TemporalAdjusters.previousOrSame(DayOfWeek.FRIDAY));

        }
        if (weekId == 1){
            // 获取下一周的周一和周五日期
            monday = today.with(TemporalAdjusters.next(DayOfWeek.MONDAY));
            tuesday = today.with(TemporalAdjusters.next(DayOfWeek.TUESDAY));
            wednesday = today.with(TemporalAdjusters.next(DayOfWeek.WEDNESDAY));
            thursday = today.with(TemporalAdjusters.next(DayOfWeek.THURSDAY));
            friday = monday.with(TemporalAdjusters.nextOrSame(DayOfWeek.FRIDAY));
        }

        Date[] dateList = new Date[5];
        dateList[0] = Date.valueOf(monday);
        dateList[1] = Date.valueOf(tuesday);
        dateList[2] = Date.valueOf(wednesday);
        dateList[3] = Date.valueOf(thursday);
        dateList[4] = Date.valueOf(friday);
        return dateList;
    }





}
